/*
    使用线程计算范围和
    线程的参数和返回值，不仅仅可以用来进行传递一般参数，也可以传递对象。
*/

#include <iostream>
#include <unistd.h>

using namespace std;

class Request
{
public:
    Request(int start, int end, const string& threadname)
    :_start(start)
    ,_end(end)
    ,_threadname(threadname)
    {}
public:
    int _start;
    int _end;
    string _threadname;
};

class Response
{
public:
    Response(int result, int exitcode)
    :_result(result)
    ,_exitcode(exitcode)
    {}
public:
    int _result;  // 计算结果
    int _exitcode; // 计算结果是否可靠
};

void* sumCount(void * args)
{
    Request* rq = static_cast<Request*>(args);
    Response* rsp = new Response(0, 0);
    for(int i = rq->_start; i <= rq->_end; ++i)
    {
        cout << rq->_threadname << " id running, caling..., " << i << endl;
        rsp->_result += i;
        usleep(100000);
    }
    delete rq;
    return rsp;
}

int main()
{
    pthread_t tid;
    Request* rq = new Request(1, 100, "thread 1");

    pthread_create(&tid, nullptr, sumCount, rq);

    void* ret;
    pthread_join(tid, &ret);

    Response* rsp = static_cast<Response*>(ret);

    cout << "rsp->result: " << rsp->_result << ", exitcode: "<< rsp->_exitcode << endl;
    delete rsp;
    return 0;
}